<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/*
<span class='line'>  2</span> ********************************************************************************
<span class='line'>  3</span> jOWE - javascript Opensource Word Engine
<span class='line'>  4</span> http://code.google.com/p/jowe/
<span class='line'>  5</span> ********************************************************************************
<span class='line'>  6</span> 
<span class='line'>  7</span> Copyright (c) 2010-2012 Ludovic L.
<span class='line'>  8</span> 
<span class='line'>  9</span> Permission is hereby granted, free of charge, to any person obtaining
<span class='line'> 10</span> a copy of this software and associated documentation files (the
<span class='line'> 11</span> "Software"), to deal in the Software without restriction, including
<span class='line'> 12</span> without limitation the rights to use, copy, modify, merge, publish,
<span class='line'> 13</span> distribute, sublicense, and/or sell copies of the Software, and to
<span class='line'> 14</span> permit persons to whom the Software is furnished to do so, subject to
<span class='line'> 15</span> the following conditions:
<span class='line'> 16</span> The above copyright notice and this permission notice shall be
<span class='line'> 17</span> included in all copies or substantial portions of the Software.
<span class='line'> 18</span> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
<span class='line'> 19</span> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<span class='line'> 20</span> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
<span class='line'> 21</span> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
<span class='line'> 22</span> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
<span class='line'> 23</span> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
<span class='line'> 24</span> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<span class='line'> 25</span> 
<span class='line'> 26</span> ********************************************************************************
<span class='line'> 27</span> This file is an total adaptation/rewrite of the files "generate_terrain.c" and
<span class='line'> 28</span> "global_tools.c" (for the smoothmap function) from HME project
<span class='line'> 29</span> (Height Map Editor) available at http://sourceforge.net/projects/hme/
<span class='line'> 30</span> Thanks to Radu Privantu for the HME project and to Stefan Hellkvist for the
<span class='line'> 31</span> SDL plasma code.
<span class='line'> 32</span> 
<span class='line'> 33</span> ********************************************************************************
<span class='line'> 34</span> 
<span class='line'> 35</span> Details about generation time in milliseconds - for 'doMap()' (beware it's average time) :
<span class='line'> 36</span> Execution time could be extremely different depending on your configuration and many other
<span class='line'> 37</span> several factors, you would have to be really careful by interpreting the results below.
<span class='line'> 38</span> _______________________________________________________________________________ _ _ _ _ _ _ _ _
<span class='line'> 39</span> |                         |                   |         |         |           |               |
<span class='line'> 40</span> | Configuration           | Navigator         | 256x256 | 512x512 | 1024x1024 | 2048x2048(*3) |
<span class='line'> 41</span> |_________________________|___________________|_________|_________|___________|_ _ _ _ _ _ _ _|
<span class='line'> 42</span> | Intel T2400@1.83Ghz/2Gb | Google Chrome 8.0 |    50   |   150   |    550    |     2100      |
<span class='line'> 43</span> |                         | Firefox 4.0b9     |    60   |   220   |    800    |     3300(*1)  |
<span class='line'> 44</span> |                         | Firefox 3.6 (*2)  |   180   |   650   |   2400    |       -       |
<span class='line'> 45</span> |                         |                   |         |         |           |               |
<span class='line'> 46</span> | Intel i5@2.67Ghz/4Gb    | Google Chrome 15  |    25   |    60   |    200    |      700      |
<span class='line'> 47</span> |                         | Firefox 7         |    50   |   140   |    450    |       -       |
<span class='line'> 48</span> |                         | I.E. 9            |    20   |    60   |    230    |      970      |
<span class='line'> 49</span> |                         |                   |         |         |           |               |
<span class='line'> 50</span> 
<span class='line'> 51</span> (*1) After several calls, generation of large maps takes a lot of time (memory issue?).
<span class='line'> 52</span> It does not occur with smaller ones.
<span class='line'> 53</span> (*2) It works and time is correct, but it needs to optimize the jowe-ui, as displaying grid
<span class='line'> 54</span> takes time when there are a lot of cell (time is correct with big zoom).
<span class='line'> 55</span> (*3) On 2011-02-01, I decided to lower the limit of the map size to avoid long generation time.
<span class='line'> 56</span> Nonsense if it's not usable. Time for 2048x2048 map is left only for information purpose.
<span class='line'> 57</span> 
<span class='line'> 58</span> Here is a bunch of time (in ms) that I obtain with several calls in Google Chrome 8.0 (for 2000x2000) :
<span class='line'> 59</span> ~previous release :
<span class='line'> 60</span> initialize    =  172    147     95    103
<span class='line'> 61</span> make          = 1692   1711   1722   1703
<span class='line'> 62</span> smooth        =  811    786    793    773
<span class='line'> 63</span> crop          =  135    186    185    185
<span class='line'> 64</span> doMap (Total) = 2810   2830   2795   2764
<span class='line'> 65</span> ~current release :
<span class='line'> 66</span> initialize    =  250    280    212    219
<span class='line'> 67</span> make          =  999   1507   1014   1003
<span class='line'> 68</span> smooth        =  700    706    702    697
<span class='line'> 69</span> crop          =   42     48     43     44
<span class='line'> 70</span> doMap (Total) = 1991   2541   1971   1963
<span class='line'> 71</span> => Good improvements in crop and make.
<span class='line'> 72</span> => Small one in smooth
<span class='line'> 73</span> => Regression in initialize (?!)
<span class='line'> 74</span> ********************************************************************************
<span class='line'> 75</span> 
<span class='line'> 76</span> NOTICE :
<span class='line'> 77</span> - Every piece of code in here is only for the HeightMap object. No other object!
<span class='line'> 78</span> 
<span class='line'> 79</span> */</span><span class="WHIT">
<span class='line'> 80</span> 
<span class='line'> 81</span> </span><span class="STRN">"use strict"</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 82</span> 
<span class='line'> 83</span> </span><span class="COMM">/**
<span class='line'> 84</span>  * @fileOverview This file contains the HeightMap object (&lt;a href="http://code.google.com/p/jowe/">jOWE&lt;/a>).
<span class='line'> 85</span>  * @author Ludovic Lefebvre
<span class='line'> 86</span>  * @version 1.0
<span class='line'> 87</span>  */</span><span class="WHIT">
<span class='line'> 88</span> 
<span class='line'> 89</span> </span><span class="COMM">/**
<span class='line'> 90</span>  * Creates a new random height map generator object.
<span class='line'> 91</span>  * @class Represents an HeightMap.
<span class='line'> 92</span>  * @see How to use the &lt;a href="http://jowe.ouebfrance.com/examples-jowe-core.html">HeightMap&lt;/a> object?
<span class='line'> 93</span>  * @param {number} [opt_pitch=8]    Maximum elevation to be used. Items in the map will have a value between 0 and opt_pitch.
<span class='line'> 94</span>  * @param {number} [opt_ratio=3.1]  Ratio to be used.
<span class='line'> 95</span>  * @param {number} [opt_width=127]  Width of the map to be generated.
<span class='line'> 96</span>  * @param {number} [opt_height=127] Height of the map to be generated.
<span class='line'> 97</span>  */</span><span class="WHIT"> 
<span class='line'> 98</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">HeightMap</span><span class="PUNC">(</span><span class="NAME">opt_pitch</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_ratio</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 99</span> </span><span class="WHIT">    
<span class='line'>100</span>     </span><span class="COMM">/**
<span class='line'>101</span>      * Set minimum size for the side value of the working square.
<span class='line'>102</span>      * @type number
<span class='line'>103</span>      * @private
<span class='line'>104</span>      * @constant
<span class='line'>105</span>      */</span><span class="WHIT">
<span class='line'>106</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">MIN_SIDE</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">4</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>107</span> </span><span class="WHIT">    
<span class='line'>108</span>     </span><span class="COMM">/**
<span class='line'>109</span>      * Set maximum size for the side value of the working square.
<span class='line'>110</span>      * @type number
<span class='line'>111</span>      * @private
<span class='line'>112</span>      * @constant
<span class='line'>113</span>      */</span><span class="WHIT">
<span class='line'>114</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">MAX_SIDE</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1010</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>115</span> 
<span class='line'>116</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>117</span>      * Borders of the working height map to exclude from final result,
<span class='line'>118</span>      * because there are not processed in the smooth function.
<span class='line'>119</span>      * @type number
<span class='line'>120</span>      * @private
<span class='line'>121</span>      * @constant
<span class='line'>122</span>      */</span><span class="WHIT">
<span class='line'>123</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">CROP_SIZE</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>124</span> </span><span class="WHIT">        
<span class='line'>125</span>     </span><span class="COMM">/**
<span class='line'>126</span>      * Maximum elevation for current map [0 to p_pitch].
<span class='line'>127</span>      * &lt;br />
<span class='line'>128</span>      * You will have to adjust the color managment in "jowe-ui" to fit your elevation.
<span class='line'>129</span>      * @type number
<span class='line'>130</span>      * @private
<span class='line'>131</span>      */</span><span class="WHIT">
<span class='line'>132</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">8</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>133</span> </span><span class="WHIT">        
<span class='line'>134</span>     </span><span class="COMM">/**
<span class='line'>135</span>      * Indicates how much height difference between 2 points we can have.
<span class='line'>136</span>      * Only used in "make" method.
<span class='line'>137</span>      * &lt;br />
<span class='line'>138</span>      * By the way, combined with "p_pitch" (previous property),
<span class='line'>139</span>      * it allows to obtain very different types of map.
<span class='line'>140</span>      * &lt;br/>
<span class='line'>141</span>      * For now 3.1 is around the minimum value to use, below that you
<span class='line'>142</span>      * could obtain strange map (unmanaged cell display).
<span class='line'>143</span>      * If you put an higher value, your map will look flattened.
<span class='line'>144</span>      * @type number
<span class='line'>145</span>      * @private
<span class='line'>146</span>      */</span><span class="WHIT">
<span class='line'>147</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">p_ratio</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">3.1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>148</span> 
<span class='line'>149</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>150</span>      * Size of the current map [0 to p_side], [0 to p_side].
<span class='line'>151</span>      * &lt;br />
<span class='line'>152</span>      * Always have to be (N^2)+1 x (N^2)+1 ("diamond square" algorithm. Better looking results with squares).
<span class='line'>153</span>      * We only need to store it once (p_side = width = height).
<span class='line'>154</span>      * @type number
<span class='line'>155</span>      * @private
<span class='line'>156</span>      */</span><span class="WHIT">
<span class='line'>157</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">129</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>158</span> </span><span class="WHIT">        
<span class='line'>159</span>         </span><span class="COMM">// "real" dimension of the current object (not resized to 2^n square) and not cropped.</span><span class="WHIT">
<span class='line'>160</span> </span><span class="WHIT">        </span><span class="NAME">width</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">127</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>161</span> </span><span class="WHIT">        </span><span class="NAME">height</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">127</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>162</span> </span><span class="WHIT">        
<span class='line'>163</span>         </span><span class="COMM">// Random object/class.</span><span class="WHIT">
<span class='line'>164</span> </span><span class="WHIT">        </span><span class="NAME">rand</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>165</span> 
<span class='line'>166</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>167</span>      * Shorcut to Math function.
<span class='line'>168</span>      * &lt;br />
<span class='line'>169</span>      * Used to "speed up" calls to Math.floor() function.
<span class='line'>170</span>      * @function
<span class='line'>171</span>      * @private
<span class='line'>172</span>      */</span><span class="WHIT">
<span class='line'>173</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Math.floor</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>174</span> </span><span class="WHIT">    
<span class='line'>175</span>     </span><span class="COMM">/**
<span class='line'>176</span>      * Result array with the world map.
<span class='line'>177</span>      * @type Array
<span class='line'>178</span>      * @public
<span class='line'>179</span>      */</span><span class="WHIT">
<span class='line'>180</span> </span><span class="WHIT">    </span><span class="NAME">this.item</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>181</span> </span><span class="WHIT">    
<span class='line'>182</span>     </span><span class="COMM">/**
<span class='line'>183</span>      * Initialize the item array before calling the generator method.
<span class='line'>184</span>      * The whole item array is filled with the default value given as parameter.
<span class='line'>185</span>      * @param {number} h default value given to each item.
<span class='line'>186</span>      * @public
<span class='line'>187</span>      */</span><span class="WHIT">
<span class='line'>188</span> </span><span class="WHIT">    </span><span class="NAME">this.initialize</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">h</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>189</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">b</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>190</span> </span><span class="WHIT">        </span><span class="NAME">a.length</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">b.length</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>191</span> </span><span class="WHIT">        </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">b</span><span class="PUNC">[</span><span class="PUNC">--</span><span class="NAME">x</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">h</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>192</span> </span><span class="WHIT">        </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>193</span> </span><span class="WHIT">        </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">[</span><span class="PUNC">--</span><span class="NAME">x</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">b.slice</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>194</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>195</span> </span><span class="WHIT">        
<span class='line'>196</span>     </span><span class="COMM">/**
<span class='line'>197</span>      * Generate a random value between min and max (both included), result is floored.
<span class='line'>198</span>      * @param {number} min
<span class='line'>199</span>      * @param {number} max
<span class='line'>200</span>      * @return {number} Random value.
<span class='line'>201</span>      * @private
<span class='line'>202</span>      */</span><span class="WHIT">
<span class='line'>203</span> </span><span class="WHIT">    </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">p_randomMinMax</span><span class="PUNC">(</span><span class="NAME">min</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">max</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>204</span> </span><span class="WHIT">        </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">rand</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">max</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">min</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">min</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>205</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>206</span> 
<span class='line'>207</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>208</span>      * Set random value for each corner, needed before calling the generator.
<span class='line'>209</span>      * &lt;br />
<span class='line'>210</span>      * Require the item array to be initialized.
<span class='line'>211</span>      * @param {boolean} overwrite Indicates if existing values should be overwritten.
<span class='line'>212</span>      * @protected
<span class='line'>213</span>      */</span><span class="WHIT">
<span class='line'>214</span> </span><span class="WHIT">    </span><span class="NAME">this.fillCorners</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">overwrite</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>215</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">H</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>216</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">overwrite</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>217</span> </span><span class="WHIT">            </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_randomMinMax</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>218</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>219</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">overwrite</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>220</span> </span><span class="WHIT">            </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_randomMinMax</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>221</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>222</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">overwrite</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>223</span> </span><span class="WHIT">            </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_randomMinMax</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>224</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>225</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">overwrite</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>226</span> </span><span class="WHIT">            </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_randomMinMax</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>227</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>228</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>229</span> 
<span class='line'>230</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>231</span>      * Initialize each border of the map with the specified value.
<span class='line'>232</span>      * &lt;br />
<span class='line'>233</span>      * The item array should be initialized.
<span class='line'>234</span>      * @param {number} value Default height to be applied.
<span class='line'>235</span>      * @param {number} borderwidth Width of the border to apply the value.
<span class='line'>236</span>      * @protected
<span class='line'>237</span>      */</span><span class="WHIT">
<span class='line'>238</span> </span><span class="WHIT">    </span><span class="NAME">this.fillBorders</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">value</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">borderwidth</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>239</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">H</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">s</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>240</span> </span><span class="WHIT">        </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>241</span> </span><span class="WHIT">            </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>242</span> </span><span class="WHIT">                </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">borderwidth</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">borderwidth</span><span class="PUNC">)</span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">width</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">borderwidth</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">height</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">borderwidth</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>243</span> </span><span class="WHIT">                  </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">value</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>244</span> </span><span class="WHIT">                </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>245</span> </span><span class="WHIT">            </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>246</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>247</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>248</span> </span><span class="WHIT">    
<span class='line'>249</span>     </span><span class="COMM">/**
<span class='line'>250</span>      * Return a value added with a random delta.
<span class='line'>251</span>      * &lt;br/>
<span class='line'>252</span>      * If the delta is more than 0, it adds to "avg" a random value between "-delta" and "+delta".
<span class='line'>253</span>      * &lt;br />
<span class='line'>254</span>      * In fact it sets the height between two points. The closer there are, the lower the delta will be.
<span class='line'>255</span>      * The function ensure that the result is positive and less than the pitch (max height).
<span class='line'>256</span>      * &lt;br />
<span class='line'>257</span>      * @param {number} avg
<span class='line'>258</span>      * @param {number} delta
<span class='line'>259</span>      * @return {number}
<span class='line'>260</span>      * @private
<span class='line'>261</span>      */</span><span class="WHIT">
<span class='line'>262</span> </span><span class="WHIT">    </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">p_addDelta</span><span class="PUNC">(</span><span class="NAME">avg</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>263</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">delta</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">avg</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">delta</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">rand</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>264</span> </span><span class="WHIT">        </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">p_pitch</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">avg</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">avg</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="NAME">avg</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>265</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>266</span> </span><span class="WHIT">    
<span class='line'>267</span>     </span><span class="COMM">/**
<span class='line'>268</span>      * Generate a random map.
<span class='line'>269</span>      * &lt;br />
<span class='line'>270</span>      * Parameters indicates "top/left", "right/bottom" limits, and "middle" coordinates.
<span class='line'>271</span>      * &lt;br />
<span class='line'>272</span>      * Caution : this function only works with 2^n squares, as it goes recursively with divide by 2.
<span class='line'>273</span>      * @param {number} x1 x coordinate of the top 
<span class='line'>274</span>      * @param {number} y1
<span class='line'>275</span>      * @param {number} x2
<span class='line'>276</span>      * @param {number} y2
<span class='line'>277</span>      * @param {number} xm
<span class='line'>278</span>      * @param {number} ym
<span class='line'>279</span>      * @protected
<span class='line'>280</span>      */</span><span class="WHIT">
<span class='line'>281</span> </span><span class="WHIT">    </span><span class="NAME">this.make</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>282</span> </span><span class="WHIT">        </span><span class="COMM">// Notice : Removing floor below could produce more realistics maps (it adds more noise).</span><span class="WHIT">
<span class='line'>283</span> </span><span class="WHIT">                 </span><span class="COMM">// Should it be set by default ? Next "floor" is done in p_addDelta().</span><span class="WHIT">
<span class='line'>284</span> </span><span class="WHIT">                 </span><span class="COMM">// (caution : it takes more time on FF when removed).</span><span class="WHIT">
<span class='line'>285</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x2</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NAME">p_ratio</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>286</span> </span><span class="WHIT">            </span><span class="NAME">xmym</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">H</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>287</span> </span><span class="WHIT">            </span><span class="NAME">x1y1</span><span class="WHIT">  </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y1</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>288</span> </span><span class="WHIT">            </span><span class="NAME">x2y2</span><span class="WHIT">  </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x2</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y2</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>289</span> </span><span class="WHIT">            </span><span class="NAME">x1y2</span><span class="WHIT">  </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y2</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>290</span> </span><span class="WHIT">            </span><span class="NAME">x2y1</span><span class="WHIT">  </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x2</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>291</span> 
<span class='line'>292</span> </span><span class="WHIT">        </span><span class="COMM">// Set a random height for the middle of the current square.</span><span class="WHIT">
<span class='line'>293</span> </span><span class="WHIT">        </span><span class="COMM">// p_addDelta is called with the average height of the 4 points.</span><span class="WHIT">
<span class='line'>294</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_addDelta</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x1y1</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x1y2</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x2y2</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x2y1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">4</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>295</span> </span><span class="WHIT">        </span><span class="NAME">xmym</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>296</span> </span><span class="WHIT">  
<span class='line'>297</span>         </span><span class="COMM">// Set a random height for the middle of the hypotenuse of each triangle.</span><span class="WHIT">
<span class='line'>298</span> </span><span class="WHIT">        </span><span class="COMM">// p_addDelta is called with the average height of the 3 points.</span><span class="WHIT">
<span class='line'>299</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y1</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_addDelta</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x1y1</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x2y1</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xmym</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">3</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>300</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y2</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">xm</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y2</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_addDelta</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x1y2</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x2y2</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xmym</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">3</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>301</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x2</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x2</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_addDelta</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x2y1</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x2y2</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xmym</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">3</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>302</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">0</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x1</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">ym</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_addDelta</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x1y1</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x1y2</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xmym</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">3</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>303</span> 
<span class='line'>304</span> </span><span class="WHIT">        </span><span class="COMM">// Go on if there's space left to fill between points.</span><span class="WHIT">
<span class='line'>305</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">x2</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">x1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">y2</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">y1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>306</span> </span><span class="WHIT">            </span><span class="NAME">delta</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">xm</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">x1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>307</span> </span><span class="WHIT">            </span><span class="NAME">this.make</span><span class="PUNC">(</span><span class="NAME">xm</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>308</span> </span><span class="WHIT">            </span><span class="NAME">this.make</span><span class="PUNC">(</span><span class="NAME">x1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>309</span> </span><span class="WHIT">            </span><span class="NAME">this.make</span><span class="PUNC">(</span><span class="NAME">x1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>310</span> </span><span class="WHIT">            </span><span class="NAME">this.make</span><span class="PUNC">(</span><span class="NAME">xm</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ym</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">delta</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>311</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>312</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>313</span> 
<span class='line'>314</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>315</span>      * Set cells height to be closer to other adjacent cells.
<span class='line'>316</span>      * @protected
<span class='line'>317</span>      */</span><span class="WHIT">
<span class='line'>318</span> </span><span class="WHIT">    </span><span class="NAME">this.smooth</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>319</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">H</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">s</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xm1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">xp1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>320</span> </span><span class="WHIT">        </span><span class="COMM">// Goes through every lines and columns except first and last one,</span><span class="WHIT">
<span class='line'>321</span> </span><span class="WHIT">        </span><span class="COMM">// because "sum" below takes every items around the current one.</span><span class="WHIT">
<span class='line'>322</span> </span><span class="WHIT">        </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">xm1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">s</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>323</span> </span><span class="WHIT">            </span><span class="NAME">xp1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">H</span><span class="PUNC">[</span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>324</span> </span><span class="WHIT">            </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">s</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>325</span> </span><span class="WHIT">                </span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">xm1</span><span class="PUNC">[</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xm1</span><span class="PUNC">[</span><span class="NAME">y</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xm1</span><span class="PUNC">[</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="PUNC">[</span><span class="NAME">y</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="PUNC">[</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xp1</span><span class="PUNC">[</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xp1</span><span class="PUNC">[</span><span class="NAME">y</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">xp1</span><span class="PUNC">[</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>326</span> </span><span class="WHIT">                </span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NUMB">4</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">%</span><span class="WHIT"> </span><span class="NUMB">9</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">9</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>327</span> </span><span class="WHIT">                </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">p_pitch</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">sum</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">sum</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">p_pitch</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>328</span> </span><span class="WHIT">                </span><span class="NAME">x0</span><span class="PUNC">[</span><span class="NAME">y</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">sum</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>329</span> </span><span class="WHIT">            </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>330</span> </span><span class="WHIT">            </span><span class="NAME">xm1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>331</span> </span><span class="WHIT">            </span><span class="NAME">x0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">xp1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>332</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>333</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>334</span> 
<span class='line'>335</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>336</span>      * Crop the current map according to specific size.
<span class='line'>337</span>      * &lt;br />
<span class='line'>338</span>      * The internal "item" array will then be reduced.
<span class='line'>339</span>      * @param {number} [opt_width] Width of the cropped map.
<span class='line'>340</span>      * @param {number} [opt_height] Height of the cropped map.
<span class='line'>341</span>      * @protected
<span class='line'>342</span>      */</span><span class="WHIT">
<span class='line'>343</span> </span><span class="WHIT">    </span><span class="NAME">this.crop</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>344</span> </span><span class="WHIT">        </span><span class="COMM">// Initialise width and height if not specified.</span><span class="WHIT">
<span class='line'>345</span> </span><span class="WHIT">        </span><span class="NAME">opt_width</span><span class="WHIT">  </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">opt_width</span><span class="WHIT">  </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">opt_width</span><span class="WHIT">  </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">width</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>346</span> </span><span class="WHIT">        </span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">height</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>347</span> </span><span class="WHIT">        
<span class='line'>348</span>         </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item.slice</span><span class="PUNC">(</span><span class="NAME">CROP_SIZE</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_width</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">CROP_SIZE</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>349</span> </span><span class="WHIT">            </span><span class="NAME">hc</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">CROP_SIZE</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>350</span> </span><span class="WHIT">            </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">a.length</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>351</span> </span><span class="WHIT">        </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">--</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">slice</span><span class="PUNC">(</span><span class="NAME">CROP_SIZE</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">hc</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>352</span> </span><span class="WHIT">        </span><span class="NAME">this.item</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>353</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>354</span> 
<span class='line'>355</span> </span><span class="WHIT">    </span><span class="COMM">/**
<span class='line'>356</span>      * Copy data from another map to the current object at x, y coordinates.
<span class='line'>357</span>      * @param {array} source Source array that whose values will replace a part of the internal map array.
<span class='line'>358</span>      * @param {number} from_x X coordinate of the item array to start the copy
<span class='line'>359</span>      * @param {number} from_y Y coordinate of the item array to start the copy
<span class='line'>360</span>      * @protected
<span class='line'>361</span>      */</span><span class="WHIT">
<span class='line'>362</span> </span><span class="WHIT">    </span><span class="NAME">this.copy</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">source</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">from_x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">from_y</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>363</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">D</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.item</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">S</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">source.item</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>364</span> </span><span class="WHIT">        </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">S.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> 
<span class='line'>365</span>             </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">S</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">length</span><span class="WHIT"> </span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'>366</span> </span><span class="WHIT">                </span><span class="NAME">D</span><span class="PUNC">[</span><span class="NAME">from_x</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">from_y</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">y</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">S</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">]</span><span class="PUNC">[</span><span class="NAME">y</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>367</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>368</span> </span><span class="WHIT">    
<span class='line'>369</span>     </span><span class="COMM">/**
<span class='line'>370</span>      * This is a shortcut to call function make() with default parameters.
<span class='line'>371</span>      * &lt;br />
<span class='line'>372</span>      * All necessary variables should have been initialized.
<span class='line'>373</span>      * &lt;br />
<span class='line'>374</span>      * The "item" array will be set to "-1" if not already done.
<span class='line'>375</span>      * @protected
<span class='line'>376</span>      */</span><span class="WHIT">
<span class='line'>377</span> </span><span class="WHIT">    </span><span class="NAME">this.makeMap</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>378</span> </span><span class="WHIT">        </span><span class="COMM">// Initialize the "item" array with default height to -1</span><span class="WHIT">
<span class='line'>379</span> </span><span class="WHIT">        </span><span class="COMM">// and corners set to random values.</span><span class="WHIT">
<span class='line'>380</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.item.length</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>381</span> </span><span class="WHIT">          </span><span class="NAME">this.initialize</span><span class="PUNC">(</span><span class="PUNC">-</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>382</span> </span><span class="WHIT">          </span><span class="NAME">this.fillCorners</span><span class="PUNC">(</span><span class="KEYW">true</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>383</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>384</span> </span><span class="WHIT">        </span><span class="COMM">// Do map!</span><span class="WHIT">
<span class='line'>385</span> </span><span class="WHIT">        </span><span class="NAME">this.make</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>386</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>387</span> </span><span class="WHIT">    
<span class='line'>388</span>     </span><span class="COMM">/**
<span class='line'>389</span>      * Allow you to set a seed used to regenerate identical maps over multiple calls.
<span class='line'>390</span>      * @param {*} [opt_seed] Seed used to generate the current map.
<span class='line'>391</span>      * @protected
<span class='line'>392</span>      */</span><span class="WHIT">
<span class='line'>393</span> </span><span class="WHIT">    </span><span class="NAME">this.setAleaSeed</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_seed</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>394</span> </span><span class="WHIT">        </span><span class="COMM">// Use Alea() if exists.</span><span class="WHIT">
<span class='line'>395</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">Alea</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>396</span> </span><span class="WHIT">            </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">opt_seed</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_seed</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>397</span> </span><span class="WHIT">                </span><span class="NAME">rand</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Alea</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>398</span> </span><span class="WHIT">            </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>399</span> </span><span class="WHIT">                </span><span class="COMM">// Use "opt_seed" if any.</span><span class="WHIT">
<span class='line'>400</span> </span><span class="WHIT">                </span><span class="NAME">rand</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Alea</span><span class="PUNC">(</span><span class="NAME">opt_seed</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>401</span> </span><span class="WHIT">            </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>402</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>403</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>404</span> </span><span class="WHIT">    
<span class='line'>405</span>     </span><span class="COMM">/**
<span class='line'>406</span>      * Build a map (size is 0-based).
<span class='line'>407</span>      * &lt;br />
<span class='line'>408</span>      * doMap(5, 10) will return a map with dimension [0 .. 4][0 .. 9]
<span class='line'>409</span>      * but as we need 2 points to make a cell we'll have 4x9 cells (= 36 true cells displayed).
<span class='line'>410</span>      * @example
<span class='line'>411</span>      * var oMap = new HeightMap(99, 0.54);
<span class='line'>412</span>      * oMap.doMap(14, 34);
<span class='line'>413</span>      * @param {number} p_width
<span class='line'>414</span>      * @param {number} p_height
<span class='line'>415</span>      * @param {boolean} [bInitialize=true] Tell to the function to goes through an initialization of the map array.
<span class='line'>416</span>      *                              Then will call "SetSide", "initialize" and "fillcorners" with default values.
<span class='line'>417</span>      * @protected
<span class='line'>418</span>      */</span><span class="WHIT">
<span class='line'>419</span> </span><span class="WHIT">    </span><span class="NAME">this.doMap</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">p_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_height</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">bInitialize</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>420</span> </span><span class="WHIT">    
<span class='line'>421</span>         </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">bInitialize</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">bInitialize</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">bInitialize</span><span class="WHIT"> </span><span class="PUNC">===</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>422</span> </span><span class="WHIT">            </span><span class="COMM">// Initialize side width.</span><span class="WHIT">
<span class='line'>423</span> </span><span class="WHIT">            </span><span class="NAME">this.setSide</span><span class="PUNC">(</span><span class="NAME">p_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>424</span> </span><span class="WHIT">            
<span class='line'>425</span>             </span><span class="COMM">// To test speed, uncomment line below :</span><span class="WHIT">
<span class='line'>426</span> </span><span class="WHIT">            </span><span class="COMM">// if (isdebug) dbg_date[2] = new Date();</span><span class="WHIT">
<span class='line'>427</span> </span><span class="WHIT">            
<span class='line'>428</span>             </span><span class="COMM">// Initialize height.</span><span class="WHIT">
<span class='line'>429</span> </span><span class="WHIT">            </span><span class="NAME">this.initialize</span><span class="PUNC">(</span><span class="PUNC">-</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>430</span> </span><span class="WHIT">            </span><span class="COMM">// Initialize corners.</span><span class="WHIT">
<span class='line'>431</span> </span><span class="WHIT">            </span><span class="NAME">this.fillCorners</span><span class="PUNC">(</span><span class="KEYW">true</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>432</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>433</span> </span><span class="WHIT">        </span><span class="COMM">// To test speed, uncomment line below :</span><span class="WHIT">
<span class='line'>434</span> </span><span class="WHIT">        </span><span class="COMM">// if (isdebug) dbg_date[3] = new Date();</span><span class="WHIT">
<span class='line'>435</span> 
<span class='line'>436</span> </span><span class="WHIT">        </span><span class="COMM">// Do map!</span><span class="WHIT">
<span class='line'>437</span> </span><span class="WHIT">        </span><span class="NAME">this.make</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">/</span><span class="WHIT"> </span><span class="NUMB">2</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>438</span> </span><span class="WHIT">        
<span class='line'>439</span>         </span><span class="COMM">// To test speed, uncomment line below :</span><span class="WHIT">
<span class='line'>440</span> </span><span class="WHIT">        </span><span class="COMM">// if (isdebug) dbg_date[4] = new Date();</span><span class="WHIT">
<span class='line'>441</span> </span><span class="WHIT"> 
<span class='line'>442</span>         </span><span class="COMM">// Smooth height map to remove weird points.</span><span class="WHIT">
<span class='line'>443</span> </span><span class="WHIT">        </span><span class="NAME">this.smooth</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>444</span> </span><span class="WHIT">        
<span class='line'>445</span>         </span><span class="COMM">// To test speed, uncomment line below :</span><span class="WHIT">
<span class='line'>446</span> </span><span class="WHIT">        </span><span class="COMM">// if (isdebug) dbg_date[5] = new Date();</span><span class="WHIT">
<span class='line'>447</span> 
<span class='line'>448</span> </span><span class="WHIT">        </span><span class="COMM">// Crop the working map to get the requested map.</span><span class="WHIT">
<span class='line'>449</span> </span><span class="WHIT">        </span><span class="NAME">this.crop</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>450</span> 
<span class='line'>451</span> </span><span class="WHIT">        </span><span class="COMM">// To test speed, uncomment line below :</span><span class="WHIT">
<span class='line'>452</span> </span><span class="WHIT">        </span><span class="COMM">// if (isdebug) dbg_date[6] = new Date();</span><span class="WHIT">
<span class='line'>453</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>454</span> </span><span class="WHIT">    
<span class='line'>455</span>     </span><span class="COMM">/**
<span class='line'>456</span>      * Calculate the side of the "square" used to generate the random heightmap.
<span class='line'>457</span>      * @param {number} [opt_width]
<span class='line'>458</span>      * @param {number} [opt_height]
<span class='line'>459</span>      * @return {number} calculated side to be used
<span class='line'>460</span>      * @protected
<span class='line'>461</span>      */</span><span class="WHIT">
<span class='line'>462</span> </span><span class="WHIT">    </span><span class="NAME">this.setSide</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>463</span> </span><span class="WHIT">        </span><span class="COMM">// Default values if none provided.</span><span class="WHIT">
<span class='line'>464</span> </span><span class="WHIT">        </span><span class="COMM">// It also limits size to [MAX_SIDE]x[MAX_SIDE], to avoid big generation time.</span><span class="WHIT">
<span class='line'>465</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="NAME">undefined</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">isNaN</span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">MIN_SIDE</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">MAX_SIDE</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>466</span> </span><span class="WHIT">            </span><span class="NAME">width</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>467</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>468</span> </span><span class="WHIT">            </span><span class="NAME">width</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">127</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>469</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>470</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="NAME">undefined</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">isNaN</span><span class="PUNC">(</span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">MIN_SIDE</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">MAX_SIDE</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>471</span> </span><span class="WHIT">            </span><span class="NAME">height</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>472</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>473</span> </span><span class="WHIT">            </span><span class="NAME">height</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">127</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>474</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>475</span> </span><span class="WHIT">        </span><span class="COMM">// We'll exclude all the border lines to avoid weird point,</span><span class="WHIT">
<span class='line'>476</span> </span><span class="WHIT">        </span><span class="COMM">// so we enlarge the map size with (width+2) and (height+2).</span><span class="WHIT">
<span class='line'>477</span> </span><span class="WHIT">        </span><span class="NAME">opt_width</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">2</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">CROP_SIZE</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>478</span> </span><span class="WHIT">        </span><span class="NAME">opt_height</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NUMB">2</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="WHIT"> </span><span class="NAME">CROP_SIZE</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>479</span> 
<span class='line'>480</span> </span><span class="WHIT">        </span><span class="COMM">// Core engine (function make) works only with squares.</span><span class="WHIT">
<span class='line'>481</span> </span><span class="WHIT">        </span><span class="COMM">// (BTW, we also have better results with 2^N x 2^N maps)</span><span class="WHIT">
<span class='line'>482</span> </span><span class="WHIT">        </span><span class="COMM">// Let's keep the bigger side.</span><span class="WHIT">
<span class='line'>483</span> </span><span class="WHIT">        </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Math.max</span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>484</span> 
<span class='line'>485</span> </span><span class="WHIT">        </span><span class="COMM">// Look for closest 2^n size (better results from 2^7).</span><span class="WHIT">
<span class='line'>486</span> </span><span class="WHIT">        </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">n</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">4</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>487</span> </span><span class="WHIT">        </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">Math.pow</span><span class="PUNC">(</span><span class="NUMB">2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">n</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>488</span> </span><span class="WHIT">            </span><span class="NAME">n</span><span class="PUNC">++</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>489</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>490</span> </span><span class="WHIT">        </span><span class="COMM">// At this stage, working size will be ((Math.pow(2, n) + 1) x (Math.pow(2, n) + 1))</span><span class="WHIT">
<span class='line'>491</span> </span><span class="WHIT">        </span><span class="NAME">p_side</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Math.pow</span><span class="PUNC">(</span><span class="NUMB">2</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">n</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>492</span> </span><span class="WHIT">        
<span class='line'>493</span>         </span><span class="COMM">// Return calculated value.</span><span class="WHIT">
<span class='line'>494</span> </span><span class="WHIT">        </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">p_side</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>495</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>496</span> </span><span class="WHIT">    
<span class='line'>497</span>     </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">opt_pitch</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="NAME">undefined</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_pitch</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">isNaN</span><span class="PUNC">(</span><span class="NAME">opt_pitch</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>498</span> </span><span class="WHIT">        </span><span class="NAME">p_pitch</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">floor</span><span class="PUNC">(</span><span class="NAME">opt_pitch</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="COMM">// Just in case, pitch is floored, need to be an integer.</span><span class="WHIT">
<span class='line'>499</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>500</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">opt_ratio</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="NAME">undefined</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">opt_ratio</span><span class="WHIT"> </span><span class="PUNC">!==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">isNaN</span><span class="PUNC">(</span><span class="NAME">opt_ratio</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>501</span> </span><span class="WHIT">        </span><span class="NAME">p_ratio</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">opt_ratio</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>502</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>503</span> </span><span class="WHIT">    </span><span class="COMM">// Set width and height and calculate side for the internal square</span><span class="WHIT">
<span class='line'>504</span> </span><span class="WHIT">    </span><span class="NAME">this.setSide</span><span class="PUNC">(</span><span class="NAME">opt_width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">opt_height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>505</span> 
<span class='line'>506</span> </span><span class="WHIT">    </span><span class="COMM">// Use Alea() if exists.</span><span class="WHIT">
<span class='line'>507</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">Alea</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'undefined'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>508</span> </span><span class="WHIT">        </span><span class="NAME">rand</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Math.random</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>509</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>510</span> </span><span class="WHIT">        </span><span class="NAME">rand</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Alea</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>511</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>512</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>513</span> </span></pre></body></html>